<?php
namespace app\index\controller;
use think\Request;
use think\Db;
use think\Cache;

class Paycar extends \think\Controller
{
    public function paycar(){
        //沙盒账号khgxup5956@sandbox.com
        //沙盒密码111111
        header("Content-Type: text/html;charset=utf-8");
        $post_list = input('post.');
        // dump($post_list);die;
        require_once EXTEND_PATH.'alipay/wappay/service/AlipayTradeService.php';
        require_once EXTEND_PATH.'alipay/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php';
        require EXTEND_PATH.'alipay/config.php';
        
        // $good_id = input('get.good_id');
        // $guigetype_id = input('guigetype_id'); 
        // $ret = db('goods_attributes')->where("guigeType_id={$guigetype_id}")->find();
        // $attributes_id = $ret['id'];

        // $good_list = input('post.');
        // $list = db('goods_attributes')->alias('a')->join('goods b','a.goods_id=b.id')->field('a.price,a.jifen,b.*')->where("goods_id={$good_id}")->find();
        
        $out_trade_no = date('YmdHis').mt_rand(1000 , 9999);   
        if(isset($_GET['coupon_id'])){
            $coupon_id = input('get.coupon_id');
            for($i=0;$i<count($post_list['goods_id']);$i++){
                $data[] = [
                    'good_id' => $post_list['goods_id'][$i],
                    'address_id' => $post_list['address_id'],
                    'subject' => '购物车清单',
                    'num' => $post_list['goods_num'][$i], 
                    'total_amount' => $post_list['totalPrice'],
                    'out_trade_no' => $out_trade_no,
                    'attributes_id' => $post_list['attributes_id'][$i],
                    'coupon_id' => $coupon_id,
                ];            
            } 
        }else{
            for($i=0;$i<count($post_list['goods_id']);$i++){
                $data[] = [
                    'good_id' => $post_list['goods_id'][$i],
                    'address_id' => $post_list['address_id'],
                    'subject' => '购物车清单',
                    'num' => $post_list['goods_num'][$i], 
                    'total_amount' => $post_list['totalPrice'],
                    'out_trade_no' => $out_trade_no,
                    'attributes_id' => $post_list['attributes_id'][$i],
                ];            
            } 
        }
        
        session('dingdan_list',$data);
        //商户订单号，商户网站订单系统中唯一订单号，必填
        $out_trade_no = $data[0]['out_trade_no'];
        // $out_trade_no = '70501111111S001111119';
    
        //订单名称，必填
        $subject = $data[0]['subject'];
    
        //付款金额，必填
        $total_amount = $data[0]['total_amount'];

        //商品描述，可空
        $body = '';
        //超时时间
        $timeout_express="1m";
    
        $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
        $payRequestBuilder->setBody($body);
        $payRequestBuilder->setSubject($subject);
        $payRequestBuilder->setOutTradeNo($out_trade_no);
        $payRequestBuilder->setTotalAmount($total_amount);
        $payRequestBuilder->setTimeExpress($timeout_express);
        
        $payResponse = new \AlipayTradeService($config);
        
        $result=$payResponse->wapPay($payRequestBuilder,$config['return_url'],$config['notify_url']);
    
        return $good_id;
    }
}
